//
//  AccountLanguageVC.m
//  ImGuider
//
//  Created by llt on 2017/8/21.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "AccountLanguageVC.h"
#import "AccountCell.h"
#import "MainViewController.h"

@interface AccountLanguageVC ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *languageArray;
@property (nonatomic, copy) NSString *currentLanguage;

@property (nonatomic) AppLanguageType currentLanguageType;
@end

@implementation AccountLanguageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)setup {
    
    self.title = MYLocalizedString(@"SETTING_LANGUAGE_SET", nil);
    
    self.languageArray = @[
                           @{@"type" : @(AppLanguageJan), @"name" : @"简体中文", @"code" : @"zh-Hans"},
                           @{@"type" : @(AppLanguageFan), @"name" : @"繁體中文（台灣）", @"code" : @"zh-Hant"},
                           @{@"type" : @(AppLanguageFanXG), @"name" : @"繁體中文（香港）", @"code" : @"zh-Hant-HK"},
                           @{@"type" : @(AppLanguageEng), @"name" : @"English", @"code" : @"en"}];
    
    self.currentLanguage = [AppManager sharedManager].language;
    
    [self.tableView reloadData];
    
    [self rightBarButton:@"完成" color:[UIColor whiteColor] selector:@selector(settingLanguage)];
}

- (void)settingLanguage {
    
    
    if ([[AppManager sharedManager].language isEqualToString:self.currentLanguage]) {
        
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    [IGUtils setAppleLanguage:self.currentLanguageType];

    
    UIViewController *leftVC = [IGUtils loadViewController:@"LeftVC" storyboard:@"Account"];
    
    MainViewController *mainVC = [MainViewController new];
    mainVC.locationCity = [AppManager sharedManager].manVC.locationCity;
    
    [AppManager sharedManager].manVC = mainVC;
    
    BaseNavigationController *navigationController = [[BaseNavigationController alloc] initWithRootViewController:mainVC];
    
    LGSideMenuController *sideMenuController = [LGSideMenuController sideMenuControllerWithRootViewController:navigationController leftViewController:leftVC rightViewController:nil];
    
    sideMenuController.leftViewWidth = SCREEN_WIDTH * 4 / 5.0;
    sideMenuController.leftViewPresentationStyle = LGSideMenuPresentationStyleSlideAbove;
    
    [UIApplication sharedApplication].keyWindow.rootViewController = sideMenuController;
    
    [NetAPIManager getAppVersionBlock:nil];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.languageArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSDictionary *languageDict = self.languageArray[indexPath.row];
    
    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountLanguageCell"];
    
    [cell configWithDict:languageDict];
    
    
    BOOL choosed = [languageDict[@"code"] isEqualToString:self.currentLanguage];
    
    cell.choosed = choosed;
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    NSDictionary *languageDict = self.languageArray[indexPath.row];
    
    self.currentLanguage = languageDict[@"code"];
    self.currentLanguageType = [languageDict[@"type"] integerValue];
    
    [tableView reloadData];
}




@end
